<template>
	<div class="system-dept-container">
		<el-card shadow="hover">
			<div class="system-dept-search mb15">
				<el-input v-model="tableData.deptName" size="default" placeholder="请输入部门名称" clearable style="max-width: 180px"> </el-input>
				<el-button size="default" type="primary" class="ml10" @click="initTableData">
					<el-icon>
						<ele-Search />
					</el-icon>
					查询
				</el-button>
				<el-button size="default" type="success" class="ml10" v-auth="'btn.add'" @click="onOpenAddDept">
					<el-icon>
						<ele-Plus />
					</el-icon>
					添加
				</el-button>
			</div>
			<el-table
				:data="tableData.data"
				style="width: 100%"
				row-key="id"
				default-expand-all
				:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
				v-loading="tableData.loading"
			>
				<el-table-column type="index" label="序号" align="center" width="60" />
				<el-table-column prop="deptName" label="部门名称" align="center" show-overflow-tooltip> </el-table-column>
				<el-table-column prop="desc" label="部门描述" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="sort" label="排序" align="center" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createAt" label="创建时间" align="center" show-overflow-tooltip>
					<template #default="scope">
						{{ formatDate(new Date(scope.row.createAt),'YYYY-mm-dd HH:MM:SS') }}
					</template>
				</el-table-column>
				<el-table-column label="操作" align="center" show-overflow-tooltip width="150" v-auths="['btn.edit', 'btn.del']">
					<template #default="scope">
						<el-button size="small" type="warning" v-auth="'btn.edit'" @click="onOpenEditDept(scope.row)">编辑</el-button>
						<el-button size="small" type="danger" v-auth="'btn.del'" @click="onTabelRowDel(scope.row)">删除</el-button>
					</template>
				</el-table-column>
			</el-table>
		</el-card>
		<AddDept ref="addDeptRef"  @initTableData="initTableData" />
	</div>
</template>

<script lang="ts">
import { ref, toRefs, reactive, onMounted, defineComponent } from 'vue';
// import { ElMessageBox, ElMessage } from 'element-plus';
import AddDept from '/@/views/system/dept/component/addDept.vue';
import { formatDate } from '/@/utils/formatTime';
// 后台接口
import { systemApi } from '/@/api/system/index';

// 定义接口来定义对象的类型
interface TableDataRow {
	id: string;
	deptName: string;
	sort: number;
	desc: string;
	children?: TableDataRow[];
	createAt: string;
	status: number;
}
interface TableDataState {
	tableData: {
		data: Array<TableDataRow>;
		loading: boolean;
		deptName: string
	};
}

export default defineComponent({
	name: 'systemDept',
	components: { AddDept },
	setup() {
		const addDeptRef = ref();
		const editDeptRef = ref();
		const state = reactive<TableDataState>({
			tableData: {
				data: [],
				loading: false,
				deptName: ''
			},
		});
		// 初始化表格数据
		const initTableData = async () => {
			state.tableData.loading = true;
			let res: any = await systemApi().getDepartmentList({deptName: state.tableData.deptName})
			state.tableData.data = res.datas;
			state.tableData.loading = false;
		};
		// 打开新增菜单弹窗
		const onOpenAddDept = () => {
			addDeptRef.value.openAddDialog();
		};
		// 打开编辑菜单弹窗
		const onOpenEditDept = (row: TableDataRow) => {
			addDeptRef.value.openEditDialog(row);
		};
		// 删除当前行
		const onTabelRowDel = (row: TableDataRow) => {
			ElMessageBox.confirm(`此操作将永久删除该部门, 是否继续?`, '提示', {
				confirmButtonText: '删除',
				cancelButtonText: '取消',
				type: 'warning',
			})
				.then(async () => {
					let res:any = await systemApi().delDepartment({id: row.id});
					if(res.success){
						initTableData();
						ElMessage.success("删除成功");
					}else{
						ElMessage.error("删除失败");
					}
				})
				.catch(() => {});
		};
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		return {
			formatDate,
			addDeptRef,
			editDeptRef,
			onOpenAddDept,
			onOpenEditDept,
			onTabelRowDel,
			initTableData,
			...toRefs(state),
		};
	},
});
</script>
